<?php
/**
 * Created by PhpStorm.
 * Login: 曹旺
 * Date: 2018-03-06
 * Time: 10:57
 */

namespace app\index\model;

use think\Model;
use think\Db;

class CommodityClass extends Model
{

    /**
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * 商品分类
     * @author  曹旺
     * @data <cw>
     */
    public function commodity_class()
    {
        //商品一级分类
        $res = Db::table('nbsm_commodity_class')->field('id,class_name')->where('pid=0 and state =1')->select();
        foreach ($res as $key => $value) {
            //商品以及分类对应下的二级分类
            $res_child = Db::table('nbsm_commodity_class')->field('id,class_name')->where(['pid' => $value['id'], 'state' => 1])->select();
            //处理数据
            $res[$key]['child'] = $res_child;
            //return $res;
        }
        return $res;
    }


    /**
     * @param $id   //商品id
     * @return array|false|mixed|\PDOStatement|string|Model
     * 商品详情
     * @author  曹旺
     * @data  14:12
     */
    public function commodity_info($id,$user_discount_rate)
    {
        $res = Db::table('nbsm_commodity c,nbsm_commodity_class cs')
            ->field('c.id,c.commodity_code,c.commodity_name,c.commodity_title,c.commodity_describe
            ,c.commodity_sale_number,c.commodity_number,c.commodity_class,c.sort,c.commodity_address,c.commodity_parameter
            ,c.commodity_now_price,c.commodity_original_price,c.commodity_free_shipping,c.commodity_free_shipping
            ,c.express_way,c.preferential_promotion,c.commodity_key,c.commodity_details,c.commodity_unit,c.weight,c.commodity_url,cs.class_name')
            //->join('nbsm_commodity_class cs', 'c.commodity_class=cs.id', 'LEFT')
            ->where(['c.id' => $id, 'c.state' => 1])->where('c.commodity_class=cs.id and cs.state=1')
            ->find();
        $res['commodity_now_price']=round($res['commodity_now_price']*$user_discount_rate,2);
        if (!empty($res)) {
            $res['commodity_parameter']=json_decode($res['commodity_parameter']);
            $res_picture = Db::table('nbsm_commodity_picture')->field('url,main_figure')->where('commodity_id=' . $id)->select();
            if (!empty($res_picture)){
                $res['picture'] = $res_picture;
            }
            return $res;
        }else{
            return $res;
        }
    }

    /**
     * @param $page  //页码
     * @param $limit  //每页显示的条数
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * 商品首页
     * @author  曹旺
     * @data  17:03
     */
    public function commodity($page, $limit)
    {
        $res = Db::table('nbsm_commodity c,nbsm_commodity_picture cp')->field('c.id,cp.url')->where('c.id=cp.commodity_id and cp.main_figure=1 and c.is_upper_lower=1 and c.state=1')->page($page, $limit)->select();
        return $res;
    }

    /**
     * @param $page  //页码
     * @param $limit  //每页显示的条数
     * 商品列表
     * @author  曹旺
     * @data  17:05
     */
    public function commodityList($page, $limit,$user_discount_rate){
        $res = Db::table('nbsm_commodity')
            ->field('id,commodity_name,commodity_describe,commodity_now_price,commodity_original_price,commodity_url')
            ->where('state=1 and is_upper_lower=1')->order('sort asc')->page($page, $limit)->select();
        foreach ($res as $key=>$value){
            $res[$key]['commodity_now_price']=round($res[$key]['commodity_now_price']*$user_discount_rate,2);
        }
        return $res;
    }


    /**
     * @param $id【1,2,3】格式
     * @return array
     * 取对应id的商品
     * @author  曹旺
     * @data  16:16
     */
    public function unset_car($id,$user_discount_rate){
        $count=count($id);
        for ($i=0;$i<$count;$i++){
            $commodity=Db::table('nbsm_commodity')->field('id,commodity_name,commodity_unit,commodity_now_price,commodity_url')
                ->where(['id'=>$id[$i]])->find();
            $commodity['commodity_now_price']=round($commodity['commodity_now_price']*$user_discount_rate,2);
            $commodity_s[]=$commodity;
        }
        return $commodity_s;
    }
}